﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Trade.Journal.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Trade.Journal"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Trade.Journal;assembly=Trade.Journal"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:EditableContainer/>
    ///
    /// </summary>
    public class EditableContainer : ContentControl
    {
        static EditableContainer()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(EditableContainer), new FrameworkPropertyMetadata(typeof(EditableContainer)));
        }

        private Label PART_Label;

        public object Display
        {
            get { return GetValue(DisplayProperty); }
            set { SetValue(DisplayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Display.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DisplayProperty =
            DependencyProperty.Register("Display", typeof(object), typeof(EditableContainer), new PropertyMetadata("未设置"));



        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(EditableContainer), new PropertyMetadata(true));





        public EditableContainer()
        {
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(ContentProperty, typeof(EditableContainer));
            descriptor.AddValueChanged(this, (s, e) =>
            {
                if (Content is DatePicker)
                    SetBinding(DisplayProperty, new Binding(nameof(DatePicker.DisplayDate)) { Source = Content, StringFormat = "yyyy-MM-dd", TargetNullValue = "未设置" });
                else if (Content is ListView)
                    SetBinding(DisplayProperty, new Binding(nameof(ListView.SelectedItem)) { Source = Content, Converter = new EnumToDescriptionConverter(), TargetNullValue = "未设置" });
                else if (Content is TextBox)
                    SetBinding(DisplayProperty, new Binding(nameof(TextBox.Text)) { Source = Content, TargetNullValue = "未设置" });
                else if (Content is BarTypeSelecter)
                    SetBinding(DisplayProperty, new Binding(nameof(BarTypeSelecter.SelectedCycle)) { Source = Content, TargetNullValue = "未设置" });
                else if (Content is OpportunitySelecter)
                {
                    SetBinding(DisplayProperty, new Binding(nameof(OpportunitySelecter.Opportunity)) { Source = Content, TargetNullValue = "未设置" });
                    ContentStringFormat = "D";
                }
                else if (Content is DateTimePicker)
                {
                    SetBinding(DisplayProperty, new Binding(nameof(DateTimePicker.SelectedDateTime)) { Source = Content, TargetNullValue = "未设置" });
                    ContentStringFormat = "yyyy-MM-dd HH:mm:ss";
                }
            });
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            PART_Label = Template.FindName("PART_Label", this) as Label;
        }

    }
}
